package com.zzu.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzu.model.Course;
import com.zzu.model.Manager;
import com.zzu.model.SC;
import com.zzu.model.Student;
import com.zzu.service.CourseService;
import com.zzu.service.ManagerService;
import com.zzu.service.SCService;
import com.zzu.service.StudentService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {

    private StudentService studentService = new StudentService();
    private CourseService courseService = new CourseService();
    private SCService scService=new SCService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String method = request.getParameter("method");
        PrintWriter out = response.getWriter();
            //跳转到学生信息页面，查询学生信息，通过request传进去
            String id = request.getParameter("id");
            if(method.equals("studentInfo")) {
                Student student = studentService.findStudentById(id);
                //将josn对象传到jsp
                JSONObject json = JSONObject.fromObject(student);//将java对象转换为json对象
                String str = json.toString();//将json对象转换为字符串
                out.println(str);
            }else if(method.equals("studentGrade"))
            {
                //查询全部选修成绩
                List<SC> scList = studentService.findAllCourses(id);
                //System.out.println(scList);
                //转成json对象
                JSONArray jsonArray = JSONArray.fromObject(scList);
                //String str=jsonArray.toString();
                out.println(jsonArray);
            }else if(method.equals("courses"))
            {

                //查询全部选修成绩
                List<Course> scList = courseService.findAllCourses();
                System.out.println("------------------------------------------------");
                System.out.println(scList);
                //转成json对象
                //JSONArray jsonArray = JSONArray.fromObject(scList);
                //String str=jsonArray.toString();

                ObjectMapper mapper=new ObjectMapper();
                //转换
                String json = mapper.writeValueAsString(scList);

                out.println(json);
            }else if(method.equals("chooseCourse"))
            {

                String coId=request.getParameter("coId");
                SC sc = new SC();
                String stuId= (String) request.getSession().getAttribute("studentId");
                System.out.println("--------------------"+stuId);
                sc.setsId(stuId);
                sc.setCoId(coId);
                scService.insertSC(sc);
            }else if(method.equals("deleteCourse"))
            {
                String stuId= (String) request.getSession().getAttribute("studentId");

                //查询全部选修成绩
                List<SC> scList = studentService.findAllCourses(stuId);
                System.out.println("----------------------------");
                System.out.println(scList);
                //转成json对象
                JSONArray jsonArray = JSONArray.fromObject(scList);
                //String str=jsonArray.toString();
                out.println(jsonArray);
            }else if(method.equals("delete"))
            {
                String stuId= (String) request.getSession().getAttribute("studentId");

                String coId=request.getParameter("coId");
                SC sc = new SC();
                sc.setsId(stuId);
                sc.setCoId(coId);
                scService.deleteSC(sc);
            }

        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
